<?php

namespace app\shop\controller\product;

use think\config\driver\Json;
use think\Controller;

class Comment extends Controller
{
    protected $BusinessModel;
    protected $ProductModel;
    protected $ProCommentModel;
    protected $OrderModel;
    protected $OrderProductModel;

    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('business.Business');
        $this->ProductModel = model('product.Product');
        $this->ProCommentModel = model('product.comment.Comment');
        $this->OrderModel = model('product.order.Order');
        $this->OrderProductModel = model('product.order.Product');
    }

    //评论的商品
    public function index()
    {
        if ($this->request->isPost()) {
            $busid = $this->request->param('busid', 0, 'trim');
            $proid = $this->request->param('proid', 0, 'trim');
            $orderid = $this->request->param('orderid', 0, 'trim');
            $attrs = $this->request->param('attrs', "", 'trim');

            $business = $this->BusinessModel->find($busid);

            if (!$business) {
                $this->error('用户不存在');
            }

            $proCon = [
                'busid' => $busid,
                'proid' => $proid,
                'orderid' => $orderid,
                'attrs' => json_decode($attrs)
            ];

            $product = $this->ProductModel->where(['id' => $proid])->select();

            $data = [
                'list' => $product
            ];

            $comment = $this->ProCommentModel->where($proCon)->select();
            // halt(collection($comment)->toArray());

            if ($comment) {
                $data['list'][0]['comment'] = $comment[0]['content'];
            }

            if ($product) {
                $this->success("查找成功", null, $data);
            } else {
                $this->error("查找失败");
            }
        }
    }

    //待评价商品
    public function clist()
    {
        if ($this->request->isPost()) {
            $busid = $this->request->param('busid', 0, 'trim');
            $page = $this->request->param('page', 0, 'trim');

            $order = $this->OrderModel->where(['busid' => $busid, 'status' => 3])->select();

            if ($order) {
                $data = [];
                $datas = [];

                foreach ($order as $item) {
                    $orderPro = $this->OrderProductModel->with('products')->where(['busid' => $busid,'orderid' => $item['id']])->page($page,10)->select();
                    $orderProcount = $this->OrderProductModel->with('products')->where(['busid' => $busid,'orderid' => $item['id']])->select();

                    foreach ($orderPro as $key) {
                        $list = [];
                        $list = [
                            'busid' => $key['busid'],
                            'proid' => $key['proid'],
                            'orderid' => $key['orderid'],
                            'attrs' => $key['attrs'],
                        ];

                        $comment = $this->ProCommentModel->where($list)->select();
                        if (!$comment) {
                            $key['createtime_text'] = $item['createtime_text'];
                            array_push($data, $key);
                        }
                    }
                    foreach ($orderProcount as $key) {
                        $list = [];
                        $list = [
                            'busid' => $key['busid'],
                            'proid' => $key['proid'],
                            'orderid' => $key['orderid'],
                            'attrs' => $key['attrs'],
                        ];

                        $comment = $this->ProCommentModel->where($list)->select();
                        if (!$comment) {
                            $key['createtime_text'] = $item['createtime_text'];
                            array_push($datas, $key);
                        }
                    }
                }

                if (count($data) > 0) {
                    $params = [
                        'content' => $data,
                        'count' => count($datas)
                    ];
                    $this->success("查找成功待评价商品", null, $params);
                } else {
                    $this->error('没有待评价商品');
                }
            } else {
                $this->error('没有待评价商品');
            }
        }
    }

    //评论
    public function comment()
    {
        if ($this->request->isPost()) {
            $busid = $this->request->param('busid', 0, 'trim');
            $proid = $this->request->param('proid', 0, 'trim');
            $orderid = $this->request->param('orderid', 0, 'trim');
            $content = $this->request->param('content', '', 'trim');
            $attrs = $this->request->param('attrs', "", 'trim');

            $business = $this->BusinessModel->find($busid);
            $product = $this->ProductModel->where(['id' => $proid])->select();
            $order = $this->OrderModel->find($orderid);

            if (!$business) {
                $this->error('用户不存在');
            }
            if (!$product) {
                $this->error('商品不存在');
            }
            if (!$order) {
                $this->error('订单不存在');
            }

            $where = [
                'busid' => $busid,
                'proid' => $proid,
                'orderid' => $orderid,
                'content' => $content,
                'attrs' => json_decode($attrs)
            ];

            $comment = $this->ProCommentModel->validate('common/product/comment/Comment')->save($where);

            if ($comment) {
                $this->success("评论成功");
            } else {
                $this->error("评论失败");
            }
        }
    }
}
